package controller.business.businessInfo;

import controller.common.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.business.BusinessInfoServices;
import services.business.InspectorInfoServices;
import services.expertInfo.ExpertBaseServices;
import services.expertInfo.ExpertCertificateServices;
import util.JsonUtil;
import wt.entity.business.BusinessInfo;
import wt.entity.business.InspectorInfo;
import wt.entity.expertInfo.ExpertBase;
import wt.entity.expertInfo.ExpertCertificate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("business/businessInfo")
public class BusinessInfoController extends BaseController {
    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    ExpertCertificateServices expertCertificateServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;


    @RequestMapping("index")
    public String index(Map map) throws Exception {


        map.put("data", JsonUtil.obj2Json(businessInfoServices.selectByExample(null)));
        return "business/businessInfo/index";
    }


    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(String businessInfo) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        businessInfoServices.insertSelective((BusinessInfo) JsonUtil.json2Obj(businessInfo, BusinessInfo.class));
        return commonResult.setSuccess();
    }

    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(String businessInfo) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        businessInfoServices.updateByPrimaryKeySelective((BusinessInfo) JsonUtil.json2Obj(businessInfo, BusinessInfo.class));
        return commonResult.setSuccess();
    }


    @ResponseBody
    @RequestMapping("insertExpert")
    public CommonResult insertExpert(String idCardList, String businessId) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(idCardList)) {
            commonResult.setMessage("idCardList 为空");
            return commonResult.setError();
        }


        BusinessInfo businessInfo = businessInfoServices.selectByPrimaryKey(businessId);
        if (businessInfo == null) {
            commonResult.setMessage("businessId 未找到业务");
            return commonResult.setError();
        }


        List<InspectorInfo> inspectorInfoList = new ArrayList<InspectorInfo>();
        String[] idCards = idCardList.split(",");

        for (int i = 0; i < idCards.length; i++) {
            String idCard = idCards[i];

            ExpertBase expertBase = expertBaseServices.selectByIdCard(idCard);
            if (expertBase == null) {
                commonResult.addMessage(i + " 专家不存在");
                return commonResult.setError();
            }

            ExpertCertificate expertCertificate = expertCertificateServices.adminSelect(expertBase.getId(), businessInfo.getItemId());
            if (expertCertificate == null) {
                commonResult.addMessage(i + "的证书为空");
                return commonResult.setError();
            }

            InspectorInfo inspectorInfo = new InspectorInfo();
            inspectorInfo.setExpertId(expertBase.getId());
            inspectorInfo.setExpertIdCard(expertBase.getIdCard());
            inspectorInfo.setExpertName(expertBase.getName());
            inspectorInfo.setExpertPhone(expertBase.getPhone());
            inspectorInfo.setBankCard(expertBase.getBankCard());
            inspectorInfo.setBankName(expertBase.getBankName());
            inspectorInfo.setBusinessId(businessInfo.getId());
            inspectorInfo.setCertificatId(expertCertificate.getId());
            inspectorInfo.setCertificateTypeId(expertCertificate.getCertificateTypeId());
            inspectorInfo.setCertificateName(expertCertificate.getName());
            inspectorInfo.setCertificateNum(expertCertificate.getNum());
            inspectorInfo.setCertificateGrade(expertCertificate.getGrade());
            inspectorInfo.setCertificateStartTime(expertCertificate.getStartTime());
            inspectorInfo.setCertificateEndTime(expertCertificate.getEndTime());
            inspectorInfo.setCertificateSrc("11");
            inspectorInfo.setCreateTime(new Date());
            if (i == 0) {
                inspectorInfo.setExpertRole("leader");
            } else {
                inspectorInfo.setExpertRole("crew");
            }

            inspectorInfoList.add(inspectorInfo);

        }

        inspectorInfoServices.insertSelective(inspectorInfoList, businessInfo.getId(),null,null);
        return commonResult.setSuccess();
    }


    @RequestMapping("inspectorInfoDlg")
    public String inspectorInfoDlg(String businessId, Map map) throws Exception {

        List<InspectorInfo> inspectorInfoList = inspectorInfoServices.selectByBusinessId(businessId);

        map.put("data", JsonUtil.obj2Json(inspectorInfoList));

        return "business/businessInfo/inspectorInfoDlg";
    }


}
